<?php
/**
 * WooCommerce Archive Product Template
 * 商品列表页面模板，基于HTML模板 collection.html
 * 
 * @package WP Bootstrap Woo
 */

defined('ABSPATH') || exit;

get_header(); ?>

<!-- breadcrumb start -->
<section class="breadcrumb-area">
    <div class="container">
        <div class="col">
            <div class="row">
                <div class="breadcrumb-index">
                    <?php woocommerce_breadcrumb(); ?>
                </div>
            </div>
        </div>
    </div>
</section>
<!-- breadcrumb end -->

<!-- collection start -->
<section class="collection">
    <div class="container">
        <div class="collection-area">
            <div class="row">
                <!-- sidebar start -->
                <div class="col-12 col-lg-3 col-md-4">
                    <div class="collection-sidebar">
                        <div class="filter-block">
                            <!-- categories filter -->
                            <div class="filter-categores">
                                <div class="filter-content">
                                    <h5>Product categories</h5>
                                    <div class="filter-list-wrap">
                                        <ul class="filter-list">
                                            <?php
                                            $product_categories = get_terms(array(
                                                'taxonomy' => 'product_cat',
                                                'hide_empty' => true,
                                                'parent' => 0
                                            ));
                                            
                                            if ($product_categories && !is_wp_error($product_categories)) {
                                                foreach ($product_categories as $category) {
                                                    $count = $category->count;
                                                    echo '<li class="filter-item">';
                                                    echo '<div class="filter-checkbox">';
                                                    echo '<input type="checkbox" id="cat-' . $category->term_id . '" data-category="' . $category->slug . '">';
                                                    echo '<label for="cat-' . $category->term_id . '" class="filter-label">';
                                                    echo '<span class="filter-checkbox-mark"></span>';
                                                    echo '<span class="filter-text">' . esc_html($category->name) . ' (' . $count . ')</span>';
                                                    echo '</label>';
                                                    echo '</div>';
                                                    echo '</li>';
                                                }
                                            }
                                            ?>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                            
                            <!-- price filter -->
                            <div class="filter-price">
                                <div class="filter-content">
                                    <h5>Filter by price</h5>
                                    <div class="price-ranger">
                                        <?php
                                        // 获取价格范围
                                        $prices = wc_get_price_filter_limits();
                                        $min_price = floor($prices['min_price']);
                                        $max_price = ceil($prices['max_price']);
                                        
                                        $current_min = isset($_GET['min_price']) ? wc_clean($_GET['min_price']) : $min_price;
                                        $current_max = isset($_GET['max_price']) ? wc_clean($_GET['max_price']) : $max_price;
                                        ?>
                                        <div class="price-range-slider">
                                            <div class="price-input">
                                                <div class="field">
                                                    <input type="number" class="min-input" value="<?php echo $current_min; ?>" min="<?php echo $min_price; ?>" max="<?php echo $max_price; ?>">
                                                </div>
                                                <div class="field">
                                                    <input type="number" class="max-input" value="<?php echo $current_max; ?>" min="<?php echo $min_price; ?>" max="<?php echo $max_price; ?>">
                                                </div>
                                            </div>
                                            <div class="price-slider">
                                                <div class="price-progress"></div>
                                            </div>
                                            <div class="price-range-input">
                                                <input type="range" class="min-range" min="<?php echo $min_price; ?>" max="<?php echo $max_price; ?>" value="<?php echo $current_min; ?>">
                                                <input type="range" class="max-range" min="<?php echo $min_price; ?>" max="<?php echo $max_price; ?>" value="<?php echo $current_max; ?>">
                                            </div>
                                        </div>
                                        <button type="button" class="btn-filter-price">Filter</button>
                                    </div>
                                </div>
                            </div>
                            
                            <!-- brand filter -->
                            <div class="filter-brand">
                                <div class="filter-content">
                                    <h5>Shop by brand</h5>
                                    <div class="filter-list-wrap">
                                        <ul class="filter-list">
                                            <?php
                                            // 获取产品品牌（如果有品牌分类法）
                                            if (taxonomy_exists('product_brand')) {
                                                $brands = get_terms(array(
                                                    'taxonomy' => 'product_brand',
                                                    'hide_empty' => true
                                                ));
                                                
                                                if ($brands && !is_wp_error($brands)) {
                                                    foreach ($brands as $brand) {
                                                        $count = $brand->count;
                                                        echo '<li class="filter-item">';
                                                        echo '<div class="filter-checkbox">';
                                                        echo '<input type="checkbox" id="brand-' . $brand->term_id . '" data-brand="' . $brand->slug . '">';
                                                        echo '<label for="brand-' . $brand->term_id . '" class="filter-label">';
                                                        echo '<span class="filter-checkbox-mark"></span>';
                                                        echo '<span class="filter-text">' . esc_html($brand->name) . ' (' . $count . ')</span>';
                                                        echo '</label>';
                                                        echo '</div>';
                                                        echo '</li>';
                                                    }
                                                }
                                            } else {
                                                // 默认品牌选项
                                                $default_brands = array('Apple', 'Samsung', 'Sony', 'LG', 'Dell');
                                                foreach ($default_brands as $brand) {
                                                    echo '<li class="filter-item">';
                                                    echo '<div class="filter-checkbox">';
                                                    echo '<input type="checkbox" id="brand-' . strtolower($brand) . '" data-brand="' . strtolower($brand) . '">';
                                                    echo '<label for="brand-' . strtolower($brand) . '" class="filter-label">';
                                                    echo '<span class="filter-checkbox-mark"></span>';
                                                    echo '<span class="filter-text">' . $brand . '</span>';
                                                    echo '</label>';
                                                    echo '</div>';
                                                    echo '</li>';
                                                }
                                            }
                                            ?>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- sidebar end -->
                
                <!-- products grid start -->
                <div class="col-12 col-lg-9 col-md-8">
                    <div class="collection-content">
                        <!-- toolbar start -->
                        <div class="collection-toolbar">
                            <div class="row">
                                <div class="col-12 col-lg-6 col-md-6">
                                    <div class="collection-view-mode">
                                        <div class="view-mode-wrap">
                                            <a href="javascript:void(0)" class="view-mode view-grid active" data-view="grid">
                                                <i class="feather-grid"></i>
                                            </a>
                                            <a href="javascript:void(0)" class="view-mode view-list" data-view="list">
                                                <i class="feather-list"></i>
                                            </a>
                                        </div>
                                        <div class="collection-results">
                                            <?php woocommerce_result_count(); ?>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-12 col-lg-6 col-md-6">
                                    <div class="collection-sorting">
                                        <?php woocommerce_catalog_ordering(); ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- toolbar end -->
                        
                        <!-- products grid -->
                        <div class="collection-products">
                            <?php if (woocommerce_product_loop()) : ?>
                                
                                <?php woocommerce_product_loop_start(); ?>
                                
                                <?php while (have_posts()) : the_post(); ?>
                                    <?php wc_get_template_part('content', 'product'); ?>
                                <?php endwhile; ?>
                                
                                <?php woocommerce_product_loop_end(); ?>
                                
                            <?php else : ?>
                                
                                <div class="no-products-found">
                                    <div class="no-products-content">
                                        <h3>No products found</h3>
                                        <p>Sorry, no products were found matching your criteria.</p>
                                        <a href="<?php echo wc_get_page_permalink('shop'); ?>" class="btn-style1">Continue Shopping</a>
                                    </div>
                                </div>
                                
                            <?php endif; ?>
                        </div>
                        
                        <!-- pagination -->
                        <div class="collection-pagination">
                            <?php
                            $pagination_args = array(
                                'total'   => wc_get_loop_prop('total_pages'),
                                'current' => wc_get_loop_prop('current_page'),
                                'base'    => esc_url_raw(str_replace(999999999, '%#%', remove_query_arg('add-to-cart', get_pagenum_link(999999999, false)))),
                                'format'  => ''
                            );
                            
                            echo paginate_links($pagination_args);
                            ?>
                        </div>
                    </div>
                </div>
                <!-- products grid end -->
            </div>
        </div>
    </div>
</section>
<!-- collection end -->

<script>
// 产品筛选和排序功能
jQuery(document).ready(function($) {
    // 价格筛选
    $('.btn-filter-price').on('click', function() {
        var minPrice = $('.min-input').val();
        var maxPrice = $('.max-input').val();
        var currentUrl = new URL(window.location.href);
        
        currentUrl.searchParams.set('min_price', minPrice);
        currentUrl.searchParams.set('max_price', maxPrice);
        
        window.location.href = currentUrl.toString();
    });
    
    // 分类筛选
    $('input[data-category]').on('change', function() {
        var selectedCategories = [];
        $('input[data-category]:checked').each(function() {
            selectedCategories.push($(this).data('category'));
        });
        
        var currentUrl = new URL(window.location.href);
        if (selectedCategories.length > 0) {
            currentUrl.searchParams.set('product_cat', selectedCategories.join(','));
        } else {
            currentUrl.searchParams.delete('product_cat');
        }
        
        window.location.href = currentUrl.toString();
    });
    
    // 视图模式切换
    $('.view-mode').on('click', function() {
        $('.view-mode').removeClass('active');
        $(this).addClass('active');
        
        var viewMode = $(this).data('view');
        if (viewMode === 'list') {
            $('.collection-products').addClass('list-view');
        } else {
            $('.collection-products').removeClass('list-view');
        }
    });
});
</script>

<?php get_footer(); ?>